#include <iostream>
#define N 100

//does not fully work...breaks at maxpos in front of minpos



using namespace std;

int max_pos(int array[], int elements){
int i;
int maxpos;
for(i=0;i<elements;i++){
    if(array[i]>maxpos){
        maxpos=i;}
    }
return maxpos;
}

int min_pos(int array[], int elements){
int i;
int minpos;
for(i=0;i<elements;i++){
    if(array[i]<minpos){
        minpos=i;}
    }
return minpos;
}


void asc_sort(int array[], int start, int end){
int i, j, aux;
for(i=1; i<end; ++i){
    aux=array[i];
    j=i-1;
    while (aux<array[j] && j>start){
        array[j+1]=array[j];
        --j;}
    array[j+1]=aux;}

}

void readVector(int array[], int elements){
for (int i=0; i<elements; i++){
	cout<<"a["<<i<<"]=";
	cin>>array[i];
	cout<<"\n";}
}

void printVector(int array[], int elements){
int i;
for (i=0; i<elements; i++){
    cout<<array[i]<<" ";}
	cout<<"\n";
}

int main(){
int array[N];
int elements;
int maxpos;
int minpos;
int start;
int end;
cout<<"Type the number of elements you want in the array: ";
cin>>elements;
readVector(array,elements);

cout<<"The original array is ";
for(int i=0; i<elements; i++){
	cout<<array[i]<<" ";}
cout<<endl;

maxpos=max_pos(array, elements);
minpos=min_pos(array, elements);

if(minpos>maxpos){
    start=maxpos;
    end=minpos;}
else {
    start=minpos;
    end=maxpos;}

asc_sort(array,start, end);
printVector(array, elements);


return 0;
}


